#include "widget.h"
#include "ui_widget.h"
#include <QDateTime>
#include <QAction>
#include <QTreeWidgetItem>
#include <QStringList>
#include <QDebug>
#include <QPainter>
#include <QJsonDocument>
#include <QJsonParseError>
#include <QJsonObject>
#include <QFile>
#include "verifyruleview.h"
#include "generalconfiguerview.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    QFile file("gatewayConf.text");//读取网关配置
    if (file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        gatewaysConf = new QVector<QJsonObject>;
        while(!file.atEnd())
        {
            QJsonParseError jsonError;
            QByteArray fileData = file.readLine();
            fileData = fileData.trimmed();
            QJsonDocument jsonDoc = QJsonDocument::fromJson(fileData, &jsonError);
            if (jsonError.error == QJsonParseError::NoError && jsonDoc.isObject())
            {
                QJsonObject jsonObj = jsonDoc.object();
                gatewaysConf->append(jsonObj);
            }
        }
    }
    file.close();
    ui->areaView->setPixmap(QPixmap("./areasImage/Main.jpg"));
    QStringList hHeaderLabels;
    hHeaderLabels<<"区域"<<"监控站点"<<"告警类别"<<"告警时间";
    ui->tableWidget->setColumnCount(4);
    ui->tableWidget->setHorizontalHeaderLabels(hHeaderLabels);
    ui->tableWidget->horizontalHeader()->setStyleSheet("QHeaderView::section{background-color: rgb(200, 255, 255);border: none;height:28px;}");
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tableWidget_2->setColumnCount(2);
    ui->tableWidget_2->setRowCount(5);
    ui->tableWidget_2->horizontalHeader()->setHidden(true);
    ui->tableWidget_2->verticalHeader()->setHidden(true);
    QStringList vHeaderLabels;
    vHeaderLabels<<"监测站点数量:"<<"一级预警站点:"<<"二级预警站点:"<<"报警站点数量:"<<"故障站点数量:";
    for (int i = 0; i < vHeaderLabels.length(); i++)
    {
        QTableWidgetItem *item_1 = new QTableWidgetItem();
        item_1->setText(vHeaderLabels.at(i));
        item_1->setFlags(item_1->flags() & ~Qt::ItemIsEnabled & ~Qt::ItemIsSelectable);
        item_1->setTextColor(QColor(0, 0, 0));
        ui->tableWidget_2->setItem(i, 0, item_1);
        QTableWidgetItem *item_2 = new QTableWidgetItem();
        item_2->setFlags(item_2->flags() & ~Qt::ItemIsEnabled & ~Qt::ItemIsSelectable);
        ui->tableWidget_2->setItem(i, 1, item_2);
        item_2->setTextColor(QColor(0, 0, 0));
    }

    ui->tableWidget_2->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tableWidget_2->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tableWidget_2->verticalHeader()->setStyleSheet("QHeaderView::section{background-color:rgb(255,255,255);border:none;}");
    ui->tableWidget_2->verticalHeader()->setHighlightSections(false);
    nodeConfView = new NodeConfigureView(this);
    nodeConfView->setModal(true);
    areaConfView = new AreaConfigureView(this);
    areaConfView->setModal(true);
    logView = new LogView(this);
    logView->setModal(true);
    setWindowFlag(Qt::FramelessWindowHint);
    showFullScreen();
    timer = new QTimer(this);
    timer->start(1000);
    connect(timer, &QTimer::timeout, this, [=](){
        QDateTime currentTime = QDateTime::currentDateTime();
        QString localTime = currentTime.toString("HH:mm:ss");
        QString localYear = currentTime.toString("yyyy年MM月dd");
        QString localWeek = currentTime.toString("ddd");
        ui->timeLabel->setText(localTime);
        ui->yearLabel->setText(localYear);
        ui->weekLabel->setText(localWeek);
    });
    menu = new QMenu(this);
    QAction *action1 = new QAction("监控区域配置", this);
    QAction *action2 = new QAction("监控站点配置", this);
    QAction *action3 = new QAction("网关配置", this);
    QAction *action4 = new QAction("通用配置", this);
    menu->addAction(action1);
    menu->addAction(action2);
    menu->addAction(action3);
    menu->addAction(action4);
    connect(action1, &QAction::triggered, this, [=](){
        areaConfView->show();
    });
    connect(action2, &QAction::triggered, this, [=](){
        nodeConfView->show();
    });
    connect(action3, &QAction::triggered, this, [=](){
        GatewayConfigureView gatewayConfView(this);
        gatewayConfView.setModal(true);
        gatewayConfView.initView(gatewaysConf);
        gatewayConfView.show();
        gatewayConfView.exec();
    });
    connect(action4, &QAction::triggered, this, [=](){
        GeneralConfiguerView generalConfView(this);
        generalConfView.setModal(true);
        generalConfView.show();
        generalConfView.exec();
    });
    DBOperate dbOperate;
    nodesInfo = dbOperate.selectNodes();
    areasInfo = dbOperate.selectAreas();
    for (int i = 0; i < areasInfo.length(); i++)
    {
        QTreeWidgetItem *topLevelItem = new QTreeWidgetItem;
        QString areaName = areasInfo.at(i).areaName;
        QStringList areaPoint = areasInfo.at(i).point.split(",");
        topLevelItem->setText(0, areaName);
        QPushButton *areaButton = new QPushButton(ui->areaView);
        QString areaX = areaPoint.at(0);
        QString areaY = areaPoint.at(1);
        areaButton->resize(50, 50);
        areaButton->move(areaX.toInt(), areaY.toInt());
        areaButton->show();
        areaButton->setStyleSheet("QPushButton{border-radius: 25px; background-color: rgb(0, 255, 0)}");
        areasButton.append(areaButton);
        for (int j = 0; j < nodesInfo.length(); j++)
        {
            QString nodeName = nodesInfo.at(j).nodeName;
            QString affiliatedArea = nodesInfo.at(j).affiliatedArea;
            if (affiliatedArea == areaName)
            {
                QTreeWidgetItem *childNode = new QTreeWidgetItem;
                childNode->setText(0, nodeName);
                topLevelItem->addChild(childNode);
            }
        }
        ui->treeWidget->addTopLevelItem(topLevelItem);
    }
    areaConfView->setTable(areasInfo);
    nodeConfView->setTable(nodesInfo);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_exitButton_clicked()
{
    close();
}

void Widget::on_logButton_clicked()
{
    logView->show();
}

void Widget::on_systemSettingButton_clicked()
{
    menu->move(QCursor::pos());
    menu->show();
}

void Widget::on_expandAllButton_clicked()
{
    ui->treeWidget->expandAll();
}

void Widget::on_collapseAllButton_clicked()
{
    ui->treeWidget->collapseAll();
}

void Widget::on_accountManageButton_clicked()
{
    VerifyRuleView verifyRuleView(this);
    verifyRuleView.setModal(true);
    verifyRuleView.show();
    if (verifyRuleView.exec() != QDialog::Accepted)
    {
        Rule rule = verifyRuleView.getUserRule();
        if (rule != Rule::NotLogin)
        {
            AccountManageView accountManageView(this);
            accountManageView.setModal(true);
            accountManageView.setRule(verifyRuleView.getUserID(), rule);
            accountManageView.show();
            accountManageView.exec();
        }
    }
}
